package com.gy.paidao;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.widget.Toast;

import com.duanqu.qupai.upload.AuthService;
import com.duanqu.qupai.upload.QupaiAuthListener;
import com.gy.paidao.person.bean.LoginBean;
import com.gy.paidao.util.Contant;
import com.gy.paidao.util.GoogleLocationUtil;
import com.gy.paidao.util.LogUtil;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.Locale;

/**
 * @author GY
 * @date 16/3/30
 * @Description:
 */
public class MyApplication extends Application {

    public static String CURRENT_X = "116.477316";// 定位到的所在城市的x坐标
    public static String CURRENT_Y = "39.963053";// 定位到的所在城市的y坐标
    public static String ADDRESS;//定位到的位置
    public static String language;
    public static LoginBean loginBean;
    public static int USERID;//用户id

    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).memoryCacheExtraOptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽
                .diskCacheExtraOptions(480, 800, null)  // 本地缓存的详细信息(缓存的最大长宽)，最好不要设置这个
                .threadPoolSize(3) // default  线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级4.tasksProcessingOrder(QueueProcessingType.FIFO) // default
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
                .memoryCacheSize(4 * 1024 * 1024)  // 内存缓存的最大值
                .memoryCacheSizePercentage(13) // default
                .diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
                .diskCacheFileCount(100)  // 可以缓存的文件数量
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
                .writeDebugLogs() // 打印debug log
                .build();
        ;
        ImageLoader.getInstance().init(configuration);
        Locale locale = getResources().getConfiguration().locale;
        language = locale.getLanguage();

        initAuth(getApplicationContext(), Contant.appkey, Contant.appsecret, Contant.space);
        GoogleLocationUtil.getInstance(getApplicationContext()).addLocationListener(new GoogleLocationUtil.OnLocationListener() {
            @Override
            public void onAmapLocationListener(Location aMapLocation) {
                MyApplication.CURRENT_X = aMapLocation.getLongitude() + "";
                MyApplication.CURRENT_Y = aMapLocation.getLatitude() + "";
            }

            @Override
            public void onAmapLocationFail() {

            }
        });
    }


    /**
     * 鉴权 建议只调用一次,在Application调用。在demo里面为了测试调用了多次 得到accessToken
     *
     * @param context
     * @param appKey    appkey
     * @param appsecret appsecret
     * @param space     space
     */
    private void initAuth(Context context, String appKey, String appsecret, String space) {
        AuthService service = AuthService.getInstance();
        service.setQupaiAuthListener(new QupaiAuthListener() {
            @Override
            public void onAuthError(int errorCode, String message) {
                LogUtil.e("ErrorCode" + errorCode + "message" + message);
            }

            @Override
            public void onAuthComplte(int responseCode, String responseMessage) {
                Contant.accessToken = responseMessage;
            }
        });
        service.startAuth(context, appKey, appsecret, space);
    }
}
